package com.xunlai.infra.permission.storage.entity;

import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.MappedCollection;
import org.springframework.data.relational.core.mapping.Table;

import java.io.Serial;
import java.io.Serializable;
import java.util.Date;

/**
 * @author liang
 * @date 2022/11/26 19:43
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Table("system_user_account")
public class UserAccount extends BaseEntity implements Serializable {

    @Serial
    private static final long serialVersionUID = 5505240910048423778L;
    @Id
    private Long id;
    /**
     * 账号
     */
    private String account;
    /**
     * 密码
     */
    private String password;
    /**
     * 手机号
     */
    private String mobileNumber;
    /**
     * 邮箱
     */
    private String email;
    /**
     * 身份证
     */
    private String idCard;
    /**
     * 最后登录时间
     */
    private Date lastLoginTime;
    /**
     * 最后登录ip
     */
    private String lastLoginIp;
    /**
     * 昵称
     */
    private String nickname;
    /**
     * 头像
     */
    private String portrait;
    /**
     * 状态
     */
    private Short status;

    private String invitationCode;

    @MappedCollection(idColumn = "account_id",keyColumn = "id")
    private Employee employee;

    public static UserAccount login(UserAccount account,String lastLoginIp){
        account.setLastLoginIp(lastLoginIp);
        account.setLastLoginTime(new Date());
        return account;
    }

    public static UserAccount changeStatus(UserAccount account, UserStatus status){
        account.setStatus(status.status);
        return account;
    }

    public static enum UserStatus{
        AUDIT("审核中",  0),
        NORMAL("正常", 1),
        FROZEN("冻结中", 2),
        INVALID("无效",3),
        LOCKED("锁定中",4),

        ;
        private String desc;

        private short status;

        UserStatus(String desc, int status) {
            this.desc = desc;
            this.status = (short) status;
        }

        public String getDesc() {
            return desc;
        }

        public short getStatus() {
            return status;
        }
    }
}
